package com.health.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.health.R;
import com.health.model.JBBean;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_ORGANIZE_EMPLOYEE;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.SuiFangBean;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaApplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SuifangAdapter extends RecyclerView.Adapter<SuifangAdapter.DataListViewHolder> {

    /*
    初始化布局
     */
    private final LayoutInflater mLayoutInflater;
    private static final String TAG = "SelectorAdapter";
    private Context mContext;
    String name;
    int count = 0;
    private OnClickListener onClickListener;
    private int clickNum = 0;
    private String diseaseTypes;
    private List<Boolean> isClick = new ArrayList<>();
    String sex = "";
    private List<SuiFangBean> content;

    public SuifangAdapter(Context mContext, List<SuiFangBean> content, int number) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.content = content;
        count = number;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public DataListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DataListViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_sf, null));
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(final DataListViewHolder holder, final int position) {

        SuiFangBean info = content.get(position);
        List<ARCHIVE_BASEINFO> list = NovaApplication.fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class," IDCARD='"+info.getIDCARD()+"'");
        ARCHIVE_BASEINFO baseinfo = list.get(0);
        holder.tvName.setText(baseinfo.getCUSTOMERNAME());
        holder.tv_idcard.setText(baseinfo.getIDCARD());
        holder.tv_phone.setText(baseinfo.getPHONE());
        if(baseinfo.getSEX().equals("DM01-08_1")){
            holder.tv_sex.setText("男");
        }else {
            holder.tv_sex.setText("女");
        }
        if(null != baseinfo.getCREATED_DATE() && !baseinfo.getCREATED_DATE().equals("")){
            holder.tv_createtime.setText(baseinfo.getCREATED_DATE()+"");

        }
        if(null != info.getFOLLOWUP_DATE() && !info.getFOLLOWUP_DATE().equals("")) {
            holder.tv_sftime.setText(GlobalConts.dateFormat.format(info.getFOLLOWUP_DATE()));
        }
        List<BD_ORGANIZE_EMPLOYEE> doctorList = NovaApplication.getDoctorList();
        for (int i = 0; i < doctorList.size(); i++) {
            BD_ORGANIZE_EMPLOYEE bd_organize_employee = doctorList.get(i);
            if (info.getFOLLOWUPDOCTOR().equals(bd_organize_employee.getUSER_ID())) {
                holder.tv_doctor.setText(bd_organize_employee.getNAME());
            }
        }
        holder.tv_type.setText(info.getTYPE());
//        if(null!=info.getSfnum()){
            holder.tv_sfnum.setText(info.getSfnum());
//        }
        if(null!=info.getXybglcs()&&""!=info.getXybglcs()&&info.getXybglcs().length()>0){
            if("常规随访".equals(info.getXybglcs())){
                holder.tv_xybglcs.setBackgroundColor(Color.GREEN);
            }else  if("第1次控制不满意2周随访".equals(info.getXybglcs())){
                holder.tv_xybglcs.setBackgroundColor(Color.YELLOW);
            }else  if("两次控制不满意转诊随访".equals(info.getXybglcs())){
                holder.tv_xybglcs.setBackgroundColor(Color.rgb(255,165,0));
            }else  if("紧急转诊".equals(info.getXybglcs())){
                holder.tv_xybglcs.setBackgroundColor(Color.RED);
            }else{
                holder.tv_xybglcs.setBackgroundColor(Color.WHITE);
            }
        }else{
            holder.tv_xybglcs.setBackgroundColor(Color.WHITE);
        }

        if(null!=info.getCcsffl()&&""!=info.getCcsffl()&&info.getCcsffl().length()>0){
            if(!info.getCcsffl().contains("控制满意")){
                Log.e("此次随访分类",info.getIDCARD()+info.getCcsffl()+"");
                holder.tv_sffl.setBackgroundColor(Color.RED);
            }else{
                holder.tv_sffl.setBackgroundColor(Color.WHITE);
            }
        }

        holder.tv_xybglcs.setText(info.getXybglcs());
        holder.tv_sffl.setText(info.getCcsffl());

        if(null != info.getIS_UPDATE()) {
            if (!info.getIS_UPDATE().equals("N")) {
                holder.tv_sc.setText("未传");
            } else {
                holder.tv_sc.setText("已传");
                holder.tv_sc.setTextColor(mContext.getColor(R.color.appgreen));
            }
        }else{
            holder.tv_sc.setText("未传");
        }
    }

    class DataListViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tv_idcard,tv_sc;
        private TextView tv_sex,tv_phone,tv_createtime,tv_sftime,tv_doctor,tv_type,tv_sfnum,tv_xybglcs,tv_sffl;

        public DataListViewHolder(final Context mContext, View itemview) {
            super(itemview);

            tvName = (TextView) itemview.findViewById(R.id.tv_name);
            tv_idcard = (TextView) itemview.findViewById(R.id.tv_idcard);
            tv_sex = (TextView) itemview.findViewById(R.id.tv_sex);
            tv_phone = (TextView) itemview.findViewById(R.id.tv_phone);
            tv_createtime = (TextView) itemview.findViewById(R.id.tv_createtime);
            tv_sftime = (TextView) itemview.findViewById(R.id.tv_sftime);
            tv_doctor = (TextView) itemview.findViewById(R.id.tv_doctor);
            tv_type = (TextView) itemview.findViewById(R.id.tv_type);
            tv_sc = (TextView) itemview.findViewById(R.id.tv_sc);
            tv_sfnum = (TextView) itemview.findViewById(R.id.tv_sfnum);
            tv_xybglcs = (TextView) itemview.findViewById(R.id.tv_xybglcs);
            tv_sffl = (TextView) itemview.findViewById(R.id.tv_sffl);
//            itemview.setBackgroundColor();

        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public interface OnClickListener {
        void setOnClickItemListener(View view, int position, String name, String id);
    }
}
